function ActorSelector(actorsSrc){
	var oscar = [];
	var brasil = [];
	var inter = [];
	var man = [];
	var girl = [];

	var oscarSelected = true;
	var brasilSelected = true;
	var interSelected = true;
	var manSelected = true;
	var girlSelected = true;

	for(var i=0;i<actorsSrc.length;i++){
		if(actorsSrc[i].oscar){
			oscar.push(actorsSrc[i]);
		}

		if(actorsSrc[i].brazilian){
			brasil.push(actorsSrc[i]);
		}else{
			inter.push(actorsSrc[i]);
		}

		if(actorsSrc[i].male){
			man.push(actorsSrc[i]);
		}else{
			girl.push(actorsSrc[i]);
		}
	}

	this.select = function(value,checked){
		if(value==0) oscarSelected = checked;
		else if(value==1) brasilSelected = checked;
		else if(value==2) interSelected = checked;
		else if(value==3) manSelected = checked;
		else if(value==4) girlSelected = checked;

		return this.queryActors();
	}

	this.queryActors = function(){
		var filterList = [];
		if(brasilSelected){
			filterList = filterList.concat(brasil)
		}
		if(interSelected){
			filterList = filterList.concat(inter)
		}		
		if(oscarSelected){
			var fl = filterList.length;
			var incluir = true;
			for(var i=0;i<oscar.length;i++){
				incluir = true;
				for(var j=0;j<fl;j++){
					if(oscar[i].id==filterList[j].id){
						incluir = false;
						break;
					}
				}
				if(incluir) filterList.push(oscar[i]);
			}
		}		
		if(manSelected){
			var fl = filterList.length;
			var incluir = true;
			for(var i=0;i<man.length;i++){
				incluir = true;
				for(var j=0;j<fl;j++){
					if(man[i].id==filterList[j].id){
						incluir = false;
						break;
					}
				}
				if(incluir) filterList.push(man[i]);
			}
		}
		if(girlSelected){
			var fl = filterList.length;
			var incluir = true;
			for(var i=0;i<girl.length;i++){
				incluir = true;
				for(var j=0;j<fl;j++){
					if(girl[i].id==filterList[j].id){
						incluir = false;
						break;
					}
				}
				if(incluir) filterList.push(girl[i]);
			}
		}		

		return filterList;
	}

}

function MoviePool(){
	var movies = window.movies;
	var indices = createIndexArray(15);

	this.getRandomMovie = function(){
		var index = parseInt( (Math.random()*100)%(indices.length) );
		var movie = getMovieByIndex( indices[index] );
		indices.splice(index,1);

		return movie;
	}

	this.getRandomMovieByGender = function(gender,notUse){
		if(notUse){
			var allG = []
			for(var g in movies){
				if(g==gender){
					continue;	
				} 
				allG.push(g);
			}
			gender = allG[ parseInt( (Math.random()*100)%(allG.length) ) ];			
			
		}

		var index = randomIndexByGender(indices,gender);		
		var movie = getMovieByIndex( indices[index] );		
		indices.splice(index,1);
		return movie;
	}

	var randomIndexByGender = function(remIndex,gender){
		var limit1,limit2;
		if(gender=="drama"){
			limit1= 0;
			limit2 = 5;
		}else if(gender=="comedia"){
			limit1 = 5;
			limit2 =10;
		}else{
			limit1 = 10;
			limit2 = 15;
		}

		var realIndex = parseInt( (Math.random()*100)%(remIndex.length) );
		while(remIndex[realIndex]>=limit2 || remIndex[realIndex]<limit1){
			realIndex = parseInt( (Math.random()*100)%(remIndex.length) );
		}
		return realIndex;
	}

	var getMovieByIndex = function(index){
		var wordIndex,realIndex;
		if(index<5){
			wordIndex = "drama";
		}else if(index<10){
			wordIndex = "comedia";
		}else{
			wordIndex = "acao";
		}
		realIndex = index%5;	

		return window.movies[wordIndex][realIndex];
	}	

}

function ActorPool(){
	var actors = window.actors;
	var indices = createIndexArray(actors.length);

	this.getRandomActor = function(exclude){
		var novoIndices = [];
		var inclui = true;

		if(exclude!=null){
			for(var i=0;i<indices.length;i++){
				for(var j=0;j<exclude.length;j++){
					if(exclude[j]==indices[i]){
						inclui = false;
						exclude.splice(j,1);	
						break;
					}					
				}	
				if(inclui) novoIndices.push(i);
				inclui=true;			
			}
		}
		
		var index = parseInt( (Math.random()*100)%(novoIndices.length) );
		var actor = actors[ novoIndices[index] ];

		return actor;
	}
}

function CommentsPool(){
	var comments = window.comments;
	var positives = [];
	var negatives = [];	
	var indices = createIndexArray(comments.length);

	for(c in comments){
		if( comments[c].getType()==="POSITIVE" ) positives.push(comments[c]);
		else negatives.push(comments[c]);
	}

	var positivesIndices = createIndexArray(positives.length);	
	var negativesIndices = createIndexArray(negatives.length);	

	this.getRandomCommentByType = function(type,toNotRepeat){
		var index; 
		var comment;

		var theSame = true;

		while(theSame){

			theSame = false;
			if(type==="POSITIVE"){
				index = parseInt( (Math.random()*100)%(positivesIndices.length) );
				comment = positives[ positivesIndices[index] ];
				positivesIndices.splice(index,1);
			}else{
				index = parseInt( (Math.random()*100)%(negativesIndices.length) );
				comment = negatives[ negativesIndices[index] ];
				negativesIndices.splice(index,1);
			}

			for(var i=0;i<toNotRepeat.length;i++){
				if(toNotRepeat[i].getName()===comment.getName()){
					theSame=true;
					break;
				}
			}
		}

		return comment;
	}
}

function createIndexArray(max){
	var indices = [];
	for(var i=0;i<max;i++){
		indices.push(i);
	}
	return indices;
}
